package com.qingcheng.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qingcheng.dao.BrandMapper;
import com.qingcheng.entity.PageResult;
import com.qingcheng.pojo.Brand;
import com.qingcheng.service.goods.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @ProjectName: qingcheng_parent
 * @ClassName: BrandServiceImpl
 * @Time: 2019/6/20
 * @Author: zhou24
 * @Description:
 * @Experience:
 */
@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    private BrandMapper brandMapper;

    @Override
    public List<Brand> findAll() {
        return brandMapper.selectAll();
    }

    /**
     * 分页
     * 可以对方法加环绕通知，增强方法（抽取公共部分）
     * execution （* PageResult com.qingcheng.servic.*.*(..)）
     * 【权限修饰符】 返回值 【方法的全限定名】 方法名 参数 【异常类型】
     * 可以自定义插件
     *
     * @param page
     * @param size
     * @return
     */
    @Override
//  @Transactional(rollbackFor = Exception.class)
    public PageResult<Brand> findPage(int page, int size) {
        PageHelper.startPage(page, size);
        Page<Brand> brands = (Page<Brand>) brandMapper.selectAll();
        return new PageResult<Brand>(brands.getTotal(), brands.getResult());
    }

    /**
     * 品牌条件查询
     * @param searchMap 条件
     * @return list
     */
    @Override
    public List<Brand> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return brandMapper.selectByExample(example);
    }

    /**
     * 品牌条件+分页查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageResult<Brand> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page, size);
        Example example = createExample(searchMap);
        Page<Brand> brands = (Page<Brand>) brandMapper.selectByExample(example);
        return new PageResult<Brand>(brands.getTotal(), brands.getResult());
    }

    /**
     * @param id 产品id
     * @return
     */
    @Override
    public Brand findById(Integer id) {
        return brandMapper.selectByPrimaryKey(id);
    }

    /**
     * 添加品牌
     * @param brand 品牌
     */
    @Override
    public void add(Brand brand) {
        brandMapper.insert(brand);
//        brandMapper.insertSelective(brand); 保留默认值功能，上面的剔除了默认值功能
    }

    /**
     * 修改品牌
     * @param brand 品牌
     */
    @Override
    public void update(Brand brand) {
        brandMapper.updateByPrimaryKeySelective(brand);
    }

    /**
     * 根据Id删除品牌
     * @param id  品牌Id
     */
    @Override
    public void delete(Integer id) {
        brandMapper.deleteByPrimaryKey(id);
    }

    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();
        if (Objects.nonNull(searchMap)) {
            //if (searchMap != null) {
            //名称条件(模糊查询)
            if (searchMap.get("name") != null && !"".equals(searchMap.get("name"))) {
                criteria.andLike("name", "%" + (String) searchMap.get("name") + "%");
            }
            //首字母（精确查询）
            if (searchMap.get("letter") != null && !"".equals(searchMap.get("letter"))) {
                criteria.andEqualTo("letter", (String) searchMap.get("letter"));
            }
        }
        return example;
    }


}
